<template>
	<view>

		<!-- 404页面 -->
		<view class="noData" v-if="netWorkType == 'none'">
			<noData @handle="refresh"></noData>
		</view>
		
		<view style="width:100%;" v-if="netWorkType != 'none'">
			<view class="address-list">
				<view v-for="(item,index) in data.addressList" class="select-group">
					<view class="address-group" >
						<view class="left-side" @click="edit(item)">
							<view class="name-phone"><text class="default-address-tag" v-if="item.is_default == 1">默认</text>{{item.name}} {{item.tel}} <text class="iconfont icon">&#xe629;</text> </view>
							<view class="address-text">{{item.province_name}} {{item.city_name}} {{item.area_name}} {{item.detail}}</view>
						</view>
						<!-- <view class="right-side">
							<view class="select-action active"  @click="select(item)">选择</view>
						</view> -->
					</view>
					<view class="select-box">
						<view v-if="id==item.id"  class="selected">
							
							已选择
						</view>
						<view   @click="select(item)" class="select-btn" v-else>
							选择
						</view>
						<view class="del" @click="del(item)">
							删除
						</view>
					</view>
				</view>
				
				
				<!-- <view class="address-group">
					<view class="left-side">
						<view class="name-phone"><text class="default-address-tag">默认</text>小欧 18888888</view>
						<view class="address-text">广西南宁</view>
					</view>
					<view class="right-side">
						<view class="select-action active">选择</view>
					</view>
				</view> -->
			</view>
			
			<view class="footer-add-btn">
				<button class="btn" @click='toAddPage'>添加地址</button>
			</view>

		</view>
		
		<uni-popup ref="deleteAddress" type="center" custom="true" >
			<view class="check-box">
				<view class="content">
					确认删除该地址!
				</view>
				<view class="btn-box">
					<view class="btn-close btn-size" @tap="$refs.deleteAddress.close()">取消</view>
					<view class="btn-qr btn-size" @tap="delAddress">确认</view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>
<script>
	//公共api接口封装方法
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	//获取网络状态
	import {
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				isSelect: false,
				//网络状态
				networkStatus: true,
				//接口数据
				data: {
					addressList: []
				},
				id:'',
				check_id:'',
			};
		},
		components: {
			uniPopup
		},
		computed: {
			//注册网络状态
			...mapGetters(['netWorkType'])
		},
		onShow() {
			this.getList()
		},
		onLoad(e) {
			uni.getStorage({
				key: 'selectAddress',
				success: (e) => {
					this.id = e.data.id;
					console.log(e)
				
				},
			})
			if (e.type == 'select') {
				this.isSelect = true;
			}
		},
		methods: {
			// 刷新的方法
			refresh() {
				this.getList()
			},
			getList() {
				//初始常用地址
				const _this = this
				//console.log(66)				
				var opt = {
					url: '/address/getList',
					success: function(res) {
						//console.log(res)
						if (res.status == 1) {
							_this.data.addressList = res.data.list
							//console.log(_this.data.goods)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.message
							});
						}

					}
				};
				_this.$service.GetApi(opt)


			},
			edit(row) {
				const _this = this
				uni.setStorage({
					key: 'address',
					data: row,
					success() {
						// uni.navigateTo({
						// 	url: "./edit/edit?type=edit"
						// })
						_this.$refer.setVilit('/pages/address/edit/edit')
						let callback = function(url) {
							_this.$refs.popupvLogin.open()
						}
						var data = {
							type: 'edit',


						}
						// console.log(data)
						_this.$refer.toUrl(callback, data)
					}
				});

			},
			del(item){
				let _this = this
				_this.check_id = item.id
				_this.$refs.deleteAddress.open()
			},
			delAddress(){
				const _this = this
				//console.log(66)				
				var opt = {
					url: '/address/del',
					data:{
						id:_this.check_id
					},
					success: function(res) {
						//console.log(res)
						_this.$refs.deleteAddress.close()
						if (res.status == 1) {
							_this.getList()
							
							//console.log(_this.data.goods)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.message
							});
						}
				
					}
				};
				_this.$service.GetApi(opt)
			},
			toAddPage() {
				const _this = this
				// uni.navigateTo({
				// 	url: "./edit/edit?type=add"
				// })
				_this.$refer.setVilit('/pages/address/add/add')
				let callback = function(url) {
					_this.$refs.popupvLogin.open()
				}
				var data = {
					type: 'add'
				}
				_this.$refer.toUrl(callback, data)
			},
			select(row) {
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if (!this.isSelect) {
					return;
				}
				uni.setStorage({
					key: 'selectAddress',
					data: row,
					success() {
						uni.navigateBack();
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "./address.scss";
</style>
